package com.fortune.service.impl;

import com.fortune.dao.AuthorityDao;
import com.fortune.entity.AuthorityDo;
import com.fortune.service.AuthorityService;
import com.fortune.vo.authority.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    private AuthorityDao authorityDao;

    /**
     * 平台权限
     * @return
     */
    @Override
    public List<AuthorityPVo> getTreeAuthority() {
        List<AuthorityPVo> firstAuthoritys = authorityDao.getFirstAuthority();
        if (firstAuthoritys != null){
            for (int i = 0; i < firstAuthoritys.size(); i++){
                List<AuthorityPVo> secondAuthoritys = authorityDao.getSecondAuthority(firstAuthoritys.get(i).getId());
                for (int j = 0; j < secondAuthoritys.size(); j++){
                    List<AuthorityPVo> third = authorityDao.getPThirdAuthority(secondAuthoritys.get(j).getId());
                    secondAuthoritys.get(j).setAuthorityPVos(third);
                }
                firstAuthoritys.get(i).setAuthorityPVos(secondAuthoritys);
            }
        }
        return firstAuthoritys;
    }

    /**
     * 厂商权限
     * @return
     */
    @Override
    public List<AuthorityMVo> getTreeMAuthority() {
        List<AuthorityMVo> firstAuthoritys = authorityDao.getMFirstAuthority();
        if (firstAuthoritys != null){
            for (int i = 0; i < firstAuthoritys.size(); i++){
                List<AuthorityMVo> secondAuthoritys = authorityDao.getMSecondAuthority(firstAuthoritys.get(i).getId());
                for (int j = 0; j < secondAuthoritys.size(); j++){
                    List<AuthorityMVo> third = authorityDao.getMThirdAuthority(secondAuthoritys.get(j).getId());
                    secondAuthoritys.get(j).setAuthorityMVos(third);
                }
                firstAuthoritys.get(i).setAuthorityMVos(secondAuthoritys);
            }
        }
        return firstAuthoritys;
    }

    /**
     * 经销商权限
     * @return
     */
    @Override
    public List<AuthorityDVo> getTreeDAuthority() {
        List<AuthorityDVo> firstAuthoritys = authorityDao.getDFirstAuthority();
        if (firstAuthoritys != null){
            for (int i = 0; i < firstAuthoritys.size(); i++){
                List<AuthorityDVo> secondAuthoritys = authorityDao.getDSecondAuthority(firstAuthoritys.get(i).getId());
                for (int j = 0; j < secondAuthoritys.size(); j++){
                    List<AuthorityDVo> third = authorityDao.getDThirdAuthority(secondAuthoritys.get(j).getId());
                    secondAuthoritys.get(j).setAuthorityDVos(third);
                }
                firstAuthoritys.get(i).setAuthorityDVos(secondAuthoritys);
            }
        }
        return firstAuthoritys;
    }
}
